package com.example.mediaplayer.ui;

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.example.mediaplayer.R;
import com.example.mediaplayer.base.BaseApp;
import com.example.mediaplayer.domain.FileInfo;
import com.example.mediaplayer.utils.ViewUtil;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;

/** 
 * @author 作者ylh  E-mail:504367857@qq.com
 * @version 创建时间：2015-1-19 上午9:41:59 
 * 类说明 
 */
public class PlayActivity extends Activity {
	private DbUtils db;
    List<FileInfo> list;
    private ImageView play, pause, pre, next, repeat, shuffle, replay;
    private ProgressBar progressBar;
    private IntentFilter intentFilter = null;

    private static final String ACTION_PLAY = "com.share.ylh.mediaplayer.PLAY";

    private int STATE = 0;//1:play,2:psuse,3:stop;7：上一首 ,8:下一首
    //文件id
    private static int id;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play_activity);

        Intent intent = getIntent();
        if (!"".equals(intent.getStringExtra("playid"))) {
            sendOperateBroadCast();
        }
        initView();
        initEvent();

    }
    
    private void initView() {

        try {
            db = DbUtils.create(this);
            list = db.findAll(FileInfo.class);//通过类型查找
            Log.e("AAAAAAAA", list.size() + "");
        } catch (DbException e) {
            e.printStackTrace();
        }

        play = (ImageView) findViewById(R.id.playerplaybtn);
        pause = (ImageView) findViewById(R.id.playerpausebtn);
        pre = (ImageView) findViewById(R.id.playerprebtn);
        next = (ImageView) findViewById(R.id.playernextbtn);
        repeat = (ImageView) findViewById(R.id.repeat);
        shuffle = (ImageView) findViewById(R.id.shuffle);
        replay = (ImageView) findViewById(R.id.replay);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        stayPlayType();
    }
    
    /**
     * 退出进来后保持播放类型，顺序，随机，重复
     * 根据state判断
     */
    private void stayPlayType() {
        String tempstate = ViewUtil.getShardPStringByKey("STATE");
        String playstate = ViewUtil.getShardPStringByKey("playstate");
        if ("1".equals(tempstate)) {
            play.setVisibility(View.GONE);
            pause.setVisibility(View.VISIBLE);
        } else if ("2".equals(tempstate)) {
            pause.setVisibility(View.GONE);
            play.setVisibility(View.VISIBLE);
        }

        if ("4".equals(playstate)) {
            //顺序
            shuffle.setVisibility(View.GONE);
            repeat.setVisibility(View.VISIBLE);
        } else if ("5".equals(playstate)) {
            //重复
            repeat.setVisibility(View.GONE);
            replay.setVisibility(View.VISIBLE);
        } else if ("6".equals(playstate)) {
            //6
            replay.setVisibility(View.GONE);
            shuffle.setVisibility(View.VISIBLE);
        }else{
            //默认顺序播放
            shuffle.setVisibility(View.GONE);
            repeat.setVisibility(View.VISIBLE);
        }
    }
    
    private void initEvent() {

        //播放
        play.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                updateState(1);
                playing();
            }
        });

        //暂停
        pause.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                updateState(2);
                sendOperateBroadCast();
                pause.setVisibility(View.GONE);
                play.setVisibility(View.VISIBLE);

            }
        });

        //上一首
        pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                id = (id - 1) >= 0 ? (id - 1) : list.size();
                ViewUtil.setShardPString("id", id + "");//上一首下一首后刷新id
                //updateState(7);
                updateState(1);
                playing();
            }
        });

        //下一首
        next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                id = (id + 1) <= list.size() ? (id + 1) : 0;
                ViewUtil.setShardPString("id", id + "");//上一首下一首后刷新id
                //updateState(8);
                updateState(1);
                playing();

            }
        });

        //顺序->重复
        repeat.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                updatePlayState(5);
                ViewUtil.setShardPString("playstate", "5");
                //sendOperateBroadCast();
                repeat.setVisibility(View.GONE);
                replay.setVisibility(View.VISIBLE);
            }


        });

        //重复->随机
        replay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                updatePlayState(6);
                ViewUtil.setShardPString("playstate", "6");
                //sendOperateBroadCast();
                replay.setVisibility(View.GONE);
                shuffle.setVisibility(View.VISIBLE);
            }
        });


        //随机->顺序
        shuffle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                updatePlayState(4);
                ViewUtil.setShardPString("playstate","4");
                //sendOperateBroadCast();
                shuffle.setVisibility(View.GONE);
                repeat.setVisibility(View.VISIBLE);
            }
        });

        progressBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setProgress(100);
            }
        });
    }
    
    
    private void playing() {

        sendOperateBroadCast();
        play.setVisibility(View.GONE);
        pause.setVisibility(View.VISIBLE);


    }

    private void updatePlayState(int i) {
        ViewUtil.setShardPString("playstate", i + "");
    }

    private void updateState(int state) {
        STATE = state;
        ViewUtil.setShardPString("STATE", STATE + "");
    }


    private void sendOperateBroadCast(){
        Log.i("MYACT sendOperateBroadCast",STATE+"==="+id);
        id = Integer.parseInt(ViewUtil.getShardPStringByKey("id"));
        Intent intent = new Intent("com.share.ylh.mediaplayer.topreview");
        //发送广播to PlayActivity，
        BaseApp.AppContext.sendBroadcast(intent);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // 解除注册
        unregisterReceiver(receiver);
    }

    @Override
    protected void onResume() {
        super.onResume();

//        IntentFilter dynamic_filter = new IntentFilter();
//        dynamic_filter.addAction("com.share.ylh.mediaplayer.broatcast");//添加动态广播的Action
//        registerReceiver(receiver, dynamic_filter);  // 注册自定义动态广播消息

        // 接收广播
        registerReceiver(receiver, init());
    }

    private IntentFilter init() {
        if (intentFilter == null) {
            intentFilter = new IntentFilter();
            intentFilter.addAction("com.share.ylh.mediaplayer.broatcast");


        }
        return intentFilter;
    }

    //跟新进度条的广播
    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("com.share.ylh.mediaplayer.broatcast")) {
                int Current = intent.getIntExtra("Current", 0);
                int Duration = intent.getIntExtra("Duration", 0);
                progressBar.setMax(Duration);

                //Log.e("AAAAAAAA", "dddd" + Current + Duration + "");
                progressBar.setProgress(Current);
            }
        }
    };
    
}
